package cn.ehanghai.iot.back.service;

import cn.ehanghai.iot.back.domain.UserMessage;
import cn.ehanghai.iot.back.mapper.UserMessageMapper;
import cn.ehanghai.iot.common.domain.PageSet;
import cn.ehanghai.iot.vo.MessageWallVo;
import cn.ehanghai.iot.vo.UserMessageVo;
import net.ryian.orm.service.BaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Author: xumf
 * @Date: 2019/6/26 17:29
 */
@Component
public class UserMessageService extends BaseService<UserMessage, UserMessageMapper> {

    public UserMessage selectUserMessageByUserId(Long userId) {
        return mapper.getByUserMessage(userId);
    }

    public UserMessage selectUserMessageByOpenId(String openId) {
        return mapper.getByUserMessageByOpenId(openId);
    }

    public PageSet selectUserMessageListByPage(Integer pageNum, Integer pageSize, String phone, String keyword, String status,
                                               String flag, String startTime, String endTime) {

        if (pageSize == null) {
            pageSize = 10;
        }
        if (pageNum == null) {
            pageNum = 1;
        }

        PageSet page = new PageSet();
        Map<String, Object> map = new HashMap<>();

        map.put("phone", phone);
        map.put("status", status);
        map.put("flag", flag);
        map.put("keyword", keyword);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        int count = 0;

        count = mapper.getUserMessageCount(map);

        page.setCount(count);
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        int start = (pageNum - 1) * pageSize;
        map.put("start", start);
        int size = pageSize.intValue();
        map.put("start", start);
        map.put("size", size);
        List<UserMessage> list = mapper.getUserMessageList(map);
        List<UserMessageVo> voList = new ArrayList<>();
        for (UserMessage m : list) {
            UserMessageVo userMessageVo = new UserMessageVo();
            BeanUtils.copyProperties(m, userMessageVo);
            voList.add(userMessageVo);
        }
        page.setData(voList);
        return page;
    }

    public int updateUserMessage(UserMessage userMessage) {
        return mapper.updateByPrimaryKey(userMessage);
    }

    public List<MessageWallVo> selectMessageWallList() {
        List<UserMessage> list = mapper.getMessageWallList();
        List<MessageWallVo> wallList = new ArrayList<>();
        for (UserMessage m : list) {
            MessageWallVo messageWallVo = new MessageWallVo();
            messageWallVo.setContent(m.getContent());
            messageWallVo.setHead(m.getHead());
            messageWallVo.setRealName(m.getRealName());
            wallList.add(messageWallVo);
        }
        return wallList;
    }


}
